##=============================================================================
## Appendix Figure 4
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "hrbrthemes")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##----------------
# Compromise Index
plot1 <- dat %>%
  filter(wave == 0) %>% 
  rowwise() %>%
  mutate(compromise = sum(across(rus_control_crimea:rus_lang_schools))) %>% 
  dplyr::select(id, wave, compromise)

plot2 <- dat %>%
  filter(wave == 1) %>% 
  rowwise() %>%
  mutate(compromise = sum(across(rus_control_crimea:rus_lang_schools))) %>% 
  dplyr::select(id, wave, compromise)

plot <- bind_rows(plot1, plot2) %>% 
  drop_na(compromise) %>% 
  group_by(wave) %>% 
  count(compromise) %>% 
  mutate(wave = case_when(
    wave == 0 ~ "July 2022 \n n = 1,890",
    wave == 1 ~ "February 2023 \n n = 1,908")) %>%
  mutate(wave = factor(wave, levels = c("July 2022 \n n = 1,890", 
                                        "February 2023 \n n = 1,908")))
rm(plot1, plot2)

ggplot(plot, aes(x = compromise, y = n, fill = factor(wave))) + 
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_vline(xintercept = 1.78, colour = gray(1/2), lty = 2, lwd = 1) +
  labs(x = "Number of Acceptable Peace Deals", y = "Respondent Count", 
       fill = "") +
  scale_x_continuous(breaks = c(0:6))  +
  scale_fill_grey() +
  theme_bw(base_size = 20) +
  theme(legend.position = "bottom",
        legend.direction = "horizontal")
##----------------
# Compromise Index
plot1 <- dat %>%
  filter(wave == 0) %>% 
  rowwise() %>%
  mutate(compromise = sum(across(rus_control_crimea:rus_lang_schools))) %>% 
  dplyr::select(id, wave, compromise)

plot2 <- dat %>%
  filter(wave == 1) %>% 
  rowwise() %>%
  mutate(compromise = sum(across(rus_control_crimea:rus_lang_schools))) %>% 
  dplyr::select(id, wave, compromise)

plot <- bind_rows(plot1, plot2) %>% 
  drop_na(compromise) %>% 
  group_by(wave) %>% 
  count(compromise) %>% 
  mutate(wave = case_when(
    wave == 0 ~ "July 2022 \n n = 1,890",
    wave == 1 ~ "February 2023 \n n = 1,908")) %>%
  mutate(wave = factor(wave, levels = c("July 2022 \n n = 1,890", 
                                        "February 2023 \n n = 1,908")))
rm(plot1, plot2)

ggplot(plot, aes(x = compromise, y = n, fill = factor(wave))) + 
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_vline(xintercept = 1.78, colour = gray(1/2), lty = 2, lwd = 1) +
  labs(x = "Number of Acceptable Peace Deals", y = "Respondent Count", 
       fill = "") +
  scale_x_continuous(breaks = c(0:6))  +
  scale_fill_grey() +
  theme_bw(base_size = 20) +
  theme(legend.position = "bottom",
        legend.direction = "horizontal")
rm(list = ls())
##=============================================================================
## End of File
##=============================================================================